<template>
  <!-- 资讯评论审核 -->
  <div>
    <!-- 头部 -->
    <div class="header">
      <el-button @click="batchDelete(multipleSelection)" type="danger"
        >批量删除</el-button
      >
      <el-input
        style="width: 200px; margin-left: 10px"
        placeholder="请输关键字"
        v-model="content"
        clearable
      >
      </el-input>
      <el-button
        @click="query"
        style="width: 70px; text-align: center; margin-left: 10px"
        type="primary"
        >查询</el-button
      >
    </div>
    <br />
    <!-- 内容 -->
    <div>
      <el-table
        ref="multipleTable"
        :data="formData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column type="index" label="序号" align="center">
        </el-table-column>
        <el-table-column prop="comment" label="评论内容" align="center">
        </el-table-column>
        <el-table-column prop="userId" label="评论人编号" align="center">
        </el-table-column>
        <el-table-column prop="articleId" label="咨询编号" align="center">
        </el-table-column>
        <el-table-column prop="commentTime" label="评论时间" align="center">
          <template slot-scope="scope">
            <span>{{ zh(scope.row.commentTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="评论状态" align="center">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.status == '审核通过'"
              type="text"
              size="small"
              style="color: green"
              >通过</el-button
            >
            <el-button
              v-if="scope.row.status == '审核未通过'"
              type="text"
              size="small"
              style="color: green"
              >不通过</el-button
            >
            <el-button
              v-if="scope.row.status == '未审核'"
              type="text"
              size="small"
              style="color: #909399"
              >未审核</el-button
            >
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              @click="delet(scope.row.id)"
              v-if="
                scope.row.status == '审核通过' ||
                scope.row.status == '审核未通过' ||
                scope.row.status == '未审核'
              "
              type="text"
              size="small"
              style="color: orange"
              >删除</el-button
            >
            <el-button
              @click="adopt(scope.row.id, '审核通过')"
              v-if="scope.row.status == '未审核'"
              type="text"
              size="small"
              style="color: green"
              >通过</el-button
            >
            <el-button
              @click="adopt(scope.row.id, '审核未通过')"
              v-if="scope.row.status == '未审核'"
              type="text"
              size="small"
              style="color: red"
              >不通过</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 底部 -->
    <div class="paginetion">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[4, 6, 8, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { get, post } from "@/utils/request";
import moment from "moment";
export default {
  data() {
    return {
      content: "",
      // 表格
      formData: [],
      //表格勾选
      multipleSelection: [],
      //当前页数
      page: 1,
      //每页的条数
      pageSize: 8,
      //总页数
      total: 20,
      //查询参数
      queryData: {},
    };
  },
  computed: {},
  methods: {
    //获取每页信息数据
    async obtainData() {
      let data = {
        page: this.page,
        pageSize: this.pageSize,
      };
      let res = await get("/comment/pageQuery", { ...data, ...this.queryDta });
      this.formData = res.data.list;
      this.total = res.data.total;
    },
    //表格选择
    handleSelectionChange(val) {
      //  let a = [];
      //  for(let item in val ){
      //       a.push(val[item].id);
      //  }
      //   this.multipleSelection = a;
      // console.log(this.multipleSelection);
      this.multipleSelection = val.map((item) => item.id);
    },
    //分页操作
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.page = 1;
      this.obtainData();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.page = val;
      this.obtainData();
    },
    //转化时间的方法
    zh(time) {
      return moment(parseInt(time)).format("YYYY/MM/DD hh:mm:ss");
    },
    //查询
    query() {
      let data = {
        keywords: this.content,
      };
      this.queryData = data;
      this.page = 1;
      this.obtainData();
      console.log(this.queryData);
    },
    //删除
    async delet(id) {
      let res = await get("/comment/deleteById", { id });
      this.obtainData();
    },
    //通过 未审核
    async adopt(id, status) {
      let res = await get("/comment/check", { id, status });
      console.log(res);
      this.obtainData();
    },
    //批量删除
    async batchDelete(ids) {
      let data = {
        ids: this.multipleSelection.toString(),
      };
      let res = await post("/comment/batchDelete", data);

      this.obtainData();
    },
    // async delet(id) {
    //   // 发送上架请求
    //   let res = await get("/comment/deleteById", { id });
    //   // 给用户一个成功或失败的提示
    //   if (res.status == 200) {
    //     this.$message({
    //       message: res.message,
    //       type: "success",
    //     });
    //     // 重新刷新页面的数据
    //     this.getAllProductData();
    //   } else {
    //     this.$message.error(res.message);
    //   }
    // },
  },
  //生命周期钩子函数
  created() {
    this.obtainData();
  },

  mounted() {},
};
</script>
<style scoped>
.header {
  line-height: 40px;
}
.paginetion {
  position: fixed;
  bottom: 40px;
  right: 10px;
}
</style>